package com.bestcem.xm.wallet.grpc.client;

import com.alibaba.fastjson.JSON;
import com.bestcem.bp.pay.grpc.v1.services.*;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.common.core.sign.grpc.core.GrpcTemplate;
import com.bestcem.xm.wallet.grpc.client.converter.WalletWalletConverter;
import com.bestcem.xm.wallet.grpc.client.dto.ApplyInvoiceRequestDTO;
import com.bestcem.xm.wallet.grpc.client.dto.InvoiceDTO;
import com.bestcem.xm.wallet.grpc.client.dto.InvoiceRecordsRequestDTO;
import com.bestcem.xm.wallet.grpc.client.dto.ListInvoiceDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 企业钱包发票服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/18 17:37
 */
@Slf4j
@Service(value = "wallectInvoiceGrpcService")
public class WallectInvoiceGrpcService {

    @GrpcClient(GrpcClientNames.PAY)
    private InvoiceServiceGrpc.InvoiceServiceBlockingStub stub;

    @Resource
    private GrpcTemplate grpcTemplate;

    /**
     * 获取发票申请列表
     *
     * @param request 请求参数内容
     * @return com.bestcem.xm.wallet.grpc.client.dto.ListInvoiceDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/09 14:32
     */
    public ListInvoiceDTO listInvoices(InvoiceRecordsRequestDTO request) {
        if (log.isInfoEnabled()) {
            log.info("分页获取发票申请列表参数：param:{}", JSON.toJSON(request));
        }

        ListInvoicesRequest listInvoicesRequest = WalletWalletConverter.requestToListInvoicesRequest(request);
        // 加签并请求
        grpcTemplate.addSign(listInvoicesRequest, GrpcTemplate.SEVER_CODE_PAY);
        ListInvoicesResponse listInvoicesResponse = stub.listInvoices(listInvoicesRequest);
        // 校验查询结果
        if (Code.ERROR_VALUE == listInvoicesResponse.getStatus().getCodeValue()) {
            log.error("分页获取发票申请列表失败：dto:{} param:{}", listInvoicesResponse.getStatus().getMessage(),
                    JSON.toJSON(request));
            return null;
        }
        return WalletWalletConverter.responseToListInvoiceDTO(listInvoicesResponse);
    }

    /**
     * 创建发票信息
     *
     * @param request 请求参数内容
     * @return com.bestcem.xm.wallet.grpc.client.dto.InvoiceDTO
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/03/19 15:25
     */
    public InvoiceDTO createInvoice(ApplyInvoiceRequestDTO request) {
        if (log.isInfoEnabled()) {
            log.info("创建发票信息参数：param:{}", JSON.toJSON(request));
        }

        // 参数转换
        CreateInvoiceRequest createInvoiceRequest = WalletWalletConverter.requestToCreateInvoiceRequest(request);
        // 加签并请求
        grpcTemplate.addSign(createInvoiceRequest, GrpcTemplate.SEVER_CODE_PAY);
        CreateInvoiceResponse invoice = stub.createInvoice(createInvoiceRequest);
        // 校验查询结果
        if (Code.ERROR_VALUE == invoice.getStatus().getCodeValue()) {
            log.error("创建发票信息失败：dto:{} param:{}", invoice.getStatus().getMessage(),
                    JSON.toJSON(request));
            return null;
        }
        return WalletWalletConverter.responseToInvoiceDto(invoice.getInvoice());
    }

}
